package image

import (
	//"chat/config"
	"fmt"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/util/grand"
	"github.com/shiningrush/avatarbuilder"
	"github.com/shiningrush/avatarbuilder/calc"
	"image/color"
)

var getwd = "D:\\All_language\\go\\src\\chatting"

var colors = []uint32{
	0xff6200, 0x42c58e, 0x5a8de1, 0x785fe0, 0x43CD80,
	0xFFC125, 0xEE2C2C, 0xFFFF00, 0x7CFC00, 0x00BFFF,
	0x0000FF, 0xA020F0,
}

// GetHeadImage 返回绝对路径
func GetHeadImage(in string) (fileName string) {
	name := in
	//getwd, _ := os.Getwd()
	fmt.Println("getwd", getwd)
	//如果目标文件存在就返回
	if gfile.IsFile(getwd + fmt.Sprintf("\\client\\image\\%s.png", in)) {
		//fmt.Println("if is in", in)
		return getwd + fmt.Sprintf("\\client\\image\\%s.png", in)
	}

	mun := grand.N(0, len(colors)-1)
	// init avatarbuilder, you need to tell builder ttf file and how to alignment text
	ab := avatarbuilder.NewAvatarBuilder(getwd+fmt.Sprintf("\\client\\image\\%s", "msyh.ttc"), &calc.SourceHansSansSCMedium{})
	ab.SetBackgroundColorHex(colors[mun])
	ab.SetFrontgroundColor(color.White)
	ab.SetFontSize(80)
	ab.SetAvatarSize(200, 200)

	if len(name) > 3 {
		sb := []byte(name)
		sb = sb[:3]
		name = string(sb)
	}

	if err := ab.GenerateImageAndSave(name, getwd+fmt.Sprintf("\\client\\image\\%s.png", in)); err != nil {
		fmt.Println(err)
		return "default.png"
	}
	return getwd + fmt.Sprintf("\\client\\image\\%s.png", in)
}
